<?php
/*
Author: Øyvind M. Kongsengen
Project: EBBS
*//**********************Description**********************/


include 'library/init.php';
include 'library/header.php';

$error = false;
$maxheight = 200;
$maxwidth = 200;
$imagevariable = null;

if (isset ($_POST['Username'])) {
if (is_uploaded_file($_FILES['Portrait']['tmp_name'])) {
    list($width, $height) = getimagesize($_FILES['Portrait']['tmp_name']);
    $factor1 = $height/$maxheight;
    $factor2 = $width/$maxwidth;
    $factor = ($factor1>$factor2)?$factor1:$factor2;
    $new_width = $width / $factor;
    $new_height = $height / $factor;
    echo "$factor1*$factor2*$height*$width*$new_height*$new_width";
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromstring(file_get_contents($_FILES['Portrait']['tmp_name']));
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    ob_start();
    imagejpeg($image_p);
    $imagevariable = ob_get_contents();
    ob_end_clean();
  }
	$sql = 'INSERT INTO user (Username, Password, FirstName, LastName, Email, WebURL, BlogTitle, Portrait)
			VALUES(:Username, :Password, :FirstName, :LastName, :Email, :WebURL, :BlogTitle, :Portrait)';
	
	$sth = $db->prepare ($sql);
	$sth->bindParam (':Username', $_POST['Username']);
	$sth->bindParam (':Password', $_POST['Password']);
	$sth->bindParam (':FirstName', $_POST['FirstName']);
	$sth->bindParam (':LastName', $_POST['LastName']);
	$sth->bindParam (':Email', $_POST['Email']);
	$sth->bindParam (':WebURL', $_POST['WebURL']);
	$sth->bindParam (':BlogTitle', $_POST['BlogTitle']);
        $sth->bindParam (':Portrait', $imagevariable);
		$res = $sth->execute ();
	$res = $db->query ('SELECT LAST_INSERT_ID() as id');
	//$id = $db->lastInsertId();
	$row = $res->fetch();
	$id = $row['id'];
	
	if ($id == false)
			$error = 'Kunne ikke sette inn data i databasen!.';
	if (!$error) {

                       $sql2 = 'SELECT * from user where UserID =:id';
                       $sth = $db->prepare ($sql2);
                       $sth->bindParam (':id', $id);
                       $sth->execute();
                       $row = $sth->fetch();

                        $user->dologin($row['Username'], $id, $row['Admin']);
			header('location: blog.php?id='.$id);
			exit();
		}
	
}
?>



<h1>Registrere ny bruker</h1>
<?php
if ($error)
	echo "<h2 class='error'>$error</h2>\n";
?>

<form method="post" action="" enctype="multipart/form-data">
<label for="Userame">Username</label><input type="text" name="Username" 
<?php if (isset ($_POST['Username'])) echo "value='{$_POST['Userame']}'"; ?>/><br>
<label for="Password">Password</label><input type="password" name="Password"
<?php if (isset ($_POST['Password'])) echo "value='{$_POST['Password']}'"; ?>/><br>
<label for="FirstName">First Name</label><input type="text" name="FirstName" 
<?php if (isset ($_POST['FirstName'])) echo "value='{$_POST['FirstName']}'"; ?>/><br>
<label for="LastName">Last Name</label><input type="text" name="LastName" 
<?php if (isset ($_POST['LastName'])) echo "value='{$_POST['LastName']}'"; ?>/><br>
<label for="Email">Email</label><input type="text" name="Email" 
<?php if (isset ($_POST['Email'])) echo "value='{$_POST['Email']}'"; ?>/><br>
<label for="WebURL">WebURL</label><input type="text" name="WebURL" 
<?php if (isset ($_POST['WebURL'])) echo "value='{$_POST['WebURL']}'"; ?>/><br>
<label for="BlogTitle">BlogTitle</label><input type="text" name="BlogTitle" 
<?php if (isset ($_POST['BlogTitle'])) echo "value='{$_POST['BlogTitle']}'"; ?>/><br>
<label for="Portait">Bilde</label><input type="file" name="Portrait" title="bilde"><br/>


<!--<label for="land">Country</label><select size="1" name="land">
<?php/*
$res = $db->query ('SELECT * FROM land ORDER BY land');
while ($row = $res->fetch()){
			echo '<option ';
			if (isset($_POST['land']))
				if($_POST['land']==$row['id'])
						echo 'selected ';
			echo "value='{$row['id']}'>{$row['land']}</option><br/>\n";
			}*/
?>
</select><br>-->
<input type="submit" value="Opprett ny bruker"/>

</form>

<?php
include 'library/sidebar.php';
include 'library/footer.php';
?>

